package org.jeecg.modules.sssj.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.config.annotations.OrgPermissionAnnotation;
import org.jeecg.modules.ckts.entity.CktsShjd;
import org.jeecg.modules.ckts.service.ICktsShjdService;
import org.jeecg.modules.ckts.vo.CktsSssjQueryVo;
import org.jeecg.modules.ckts.vo.CktsSssjVo;
import org.jeecg.modules.fk.utils.CheckRoleUtil;
import org.jeecg.modules.qyxxora.entity.ZbjkScyszbjcQy;
import org.jeecg.modules.sssj.dto.CktsDto;
import org.jeecg.modules.sssj.dto.CktsSssjListDto;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author chensherun on 2023/4/10.
 * @version 1.0
 */
@Api(tags="实时数据-出口退税")
@RestController
@RequestMapping("/sssj/sssjCkts")
public class SssjCktsController {

    @Autowired
    private ICktsShjdService cktsShjdService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询每个企业数据汇总
     * @param queryVo
     * @return
     */
    @ApiOperation(value = "查询出口退税实时数据列表")
    @PostMapping(value = "/listCktsSssj")
    public Result<Page<CktsSssjVo>> listCktsSssj(@Validated @RequestBody CktsSssjListDto queryVo) {
        Page<CktsSssjVo> page = new Page<>(queryVo.getPageNo(), queryVo.getPageSize());
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Set<String> roleNameListString = sysUserService.getUserRolesSet(loginUser.getUsername());
        //判断是否为部门领导，若是则添加部门前缀作为查询条件
        if (CheckRoleUtil.checkIsDeptAdmin(roleNameListString)) {
            //查询本部门或下属部门
            if (StringUtils.isBlank(queryVo.getOrgCode()) || !queryVo.getOrgCode().startsWith(loginUser.getOrgCode())) {
                queryVo.setOrgCode(loginUser.getOrgCode());
            }
        } else if (!CheckRoleUtil.checkIsSysAdmin(roleNameListString)) {
            //不是部门领导，也不是管理员，则只能查询管户
            queryVo.setManageUserId(loginUser.getId());
        }
        CktsSssjQueryVo vo = new CktsSssjQueryVo();
        vo.setOrgCode(queryVo.getOrgCode());
        vo.setManageUserId(queryVo.getManageUserId());
        vo.setYearMonth(queryVo.getFfny());
        vo.setNsrmc(queryVo.getNsrmc());
        vo.setShxydm(queryVo.getShxdym());
        cktsShjdService.selecgCktsSssjPage(page, vo);
        return Result.OK(page);
    }

    @ApiOperation(value = "查询出口退税实时数据")
    @PostMapping(value = "/getCkts")
    public Result<Page<CktsShjd>> getCktsData(@Validated @RequestBody CktsDto cktsDto) {
        Page<CktsShjd> page = new Page<>(cktsDto.getPageNo(), cktsDto.getPageSize());
        LambdaQueryWrapper<CktsShjd> qw = Wrappers.lambdaQuery();
        qw.eq(CktsShjd::getNsrsbh, cktsDto.getTynsrsbh());
        qw.like(CktsShjd::getFfrq, cktsDto.getFfny() + "%");
        qw.eq(CktsShjd::getLchjmc, "发放");
        cktsShjdService.page(page, qw);
//        List<CktsShjd> records = page.getRecords();
//        if (records != null && records.size() > 0) {
//            for (CktsShjd record : records) {
//                CktsRespVo vo = new CktsRespVo();
//                BeanUtils.copyProperties(record, vo);
//            }
//        }
        return Result.OK(page);
    }
}
